﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace AutoUpdate.Behavior
{
    /// <summary>
    /// 自动滚动到最后一项
    /// </summary>
    public class AutoScrollToBottom
    {
        public static bool GetScrollToBottom(DependencyObject obj)
        {
            return (bool)obj.GetValue(ScrollToBottomProperty);
        }

        public static void SetScrollToBottom(DependencyObject obj, bool value)
        {
            obj.SetValue(ScrollToBottomProperty, value);
        }

        // Using a DependencyProperty as the backing store for ScrollToBottom.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ScrollToBottomProperty =
            DependencyProperty.RegisterAttached("ScrollToBottom", typeof(bool), typeof(AutoScrollToBottom), new PropertyMetadata(false,OnScrollToBottomPropertyChanged));

        private static void OnScrollToBottomPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as ListBox;
            var newValue =(bool) e.NewValue;
            var oldValue =(bool)e.OldValue;
            if (newValue!=oldValue)
            {
                control.ScrollIntoView(control.Items[control.Items.Count - 1]);
            }
        }
    }
}
